//
//  WXPAgreementDialogView.m
//  MexicoLoan
//
//  Created by wxp on 2025/4/23.
//

#import "WXPAgreementDialogView.h"

@interface WXPAgreementDialogView ()<UITextViewDelegate>

@end

@implementation WXPAgreementDialogView

#pragma mark - lazy
- (UIView *)viewBack{
    if (!_viewBack) {
        _viewBack = [UIView new];
        _viewBack.backgroundColor = COLOR_WHITE;
        _viewBack.userInteractionEnabled = true;
        _viewBack.width = SCREEN_WIDTH;
    }
    return _viewBack;
}
- (UIButton *)btnClose{
    if (!_btnClose) {
        _btnClose = [UIButton new];
        [_btnClose setImage:[UIImage imageNamed:@"icon-login-close"] forState:UIControlStateNormal];
        _btnClose.widthHeight = XY(54, 54);
        _btnClose.tag = 1;
        [_btnClose addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnClose;
}
- (UILabel *)labelTitle{
    if (!_labelTitle) {
        _labelTitle = [UILabel new];
        _labelTitle.textColor = COLOR_BLACK;
        _labelTitle.font = [UIFont boldSystemFontOfSize:18];
    }
    return _labelTitle;
}
- (NSString *)strAgreement{
    if (!_strAgreement) {
        _strAgreement = [NSString new];
        _strAgreement = @"El inicio de sesión en la cuenta implica su conformidad con el 《Acuerdo de Servicio al Usuario》 y la 《Política de Privacidad》";
    }
    return _strAgreement;
}
- (NSString *)strServicio{
    if (!_strServicio) {
        _strServicio = [NSString new];
        _strServicio = @"《Acuerdo de Servicio al Usuario》";
    }
    return _strServicio;
}
- (NSString *)strPrivacidad{
    if (!_strPrivacidad) {
        _strPrivacidad = [NSString new];
        _strPrivacidad = @"《Política de Privacidad》";
    }
    return _strPrivacidad;
}
- (UITextView *)labelAgreement
{
    if (_labelAgreement == nil) {
        _labelAgreement = [UITextView  new];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.strAgreement];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 2;
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedString.length)];
        [attributedString addAttribute:NSLinkAttributeName
                                 value:@"servicio"
                                 range:[[attributedString string] rangeOfString:self.strServicio]];
        [attributedString addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12]} range:[self.strAgreement rangeOfString:self.strServicio]];
        _labelAgreement.attributedText = attributedString;
        _labelAgreement.linkTextAttributes = @{NSForegroundColorAttributeName: COLOR_BLUE,
                                               NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};
        [attributedString addAttribute:NSLinkAttributeName
                                 value:@"privacidad"
                                 range:[[attributedString string] rangeOfString:self.strPrivacidad]];
        [attributedString addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12]} range:[self.strAgreement rangeOfString:self.strPrivacidad]];
        _labelAgreement.attributedText = attributedString;
        _labelAgreement.linkTextAttributes = @{NSForegroundColorAttributeName: COLOR_BLUE,
                                               NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};
        UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 0, 0);
        _labelAgreement.textContainerInset = insets;
        _labelAgreement.width = SCREEN_WIDTH-30*2;
        _labelAgreement.font = [UIFont systemFontOfSize:12];
        _labelAgreement.textAlignment = NSTextAlignmentLeft;
        [_labelAgreement sizeToFit];
        _labelAgreement.textColor = COLOR_LIGHT_GRAY;
        _labelAgreement.delegate = self;
        _labelAgreement.editable = NO;
        _labelAgreement.scrollEnabled = NO;
    }
    return  _labelAgreement;
}

- (UIButton *)btnConfirm{
    if (!_btnConfirm) {
        _btnConfirm = [UIButton new];
        _btnConfirm.backgroundColor = COLOR_PRIMARY;
        [_btnConfirm setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        _btnConfirm.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        [_btnConfirm setTitle:@"Acepta y sigue adelante" forState:UIControlStateNormal];
        _btnConfirm.widthHeight = XY(SCREEN_WIDTH-30*2, BUTTON_HEIGHT);
        [GlobalMethod setRoundView:_btnConfirm color:[UIColor clearColor] numRound:BUTTON_HEIGHT/2 width:0];
        _btnConfirm.tag = 2;
        [_btnConfirm addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnConfirm;
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.widthHeight = XY(SCREEN_WIDTH, SCREEN_HEIGHT);
        self.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.4];
        [self addSubView];
    }
    return self;
}

- (void)addSubView{
    [self addSubview:self.viewBack];
    [self.viewBack addSubview:self.btnClose];
    [self.viewBack addSubview:self.labelTitle];
    [self.viewBack addSubview:self.labelAgreement];
    [self.viewBack addSubview:self.btnConfirm];
    
    
    [self resetView];
}

#pragma mark - 点击事件
- (void)btnClick:(UIButton *)sender{
    [NSObject clickFeedback];
    [sender addClickAnimate];
    
    switch (sender.tag) {
        case 1:
        {
            [GlobalMethod buryPointWithPageName:KEY_AgreementDialog featureName:KEY_Click action:KEY_BUTTON_CLICK type:KEY_button];
            [self removeFromSuperview];
        }
            break;
        case 2:
        {
            [GlobalMethod buryPointWithPageName:KEY_AgreementDialog featureName:KEY_Click action:KEY_BUTTON_CLICK type:KEY_button];
            [self removeFromSuperview];
        }
            break;
        default:
            break;
    }
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(nonnull NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction{
    [NSObject clickFeedback];
    NSRange rangeServicio = [self.strAgreement rangeOfString:self.strServicio];
    if (NSEqualRanges(characterRange, rangeServicio)) {
        [Global_Nav pushVCName:@"WXPRegisterAgreementVC" animated:true];
    }
    NSRange rangePrivacidad = [self.strAgreement rangeOfString:self.strPrivacidad];
    if (NSEqualRanges(characterRange, rangePrivacidad)) {
        [Global_Nav pushVCName:@"WXPPrivacyAgreementVC" animated:true];
    }
    return YES;
}

#pragma mark - 刷新View
- (void)resetView{
    self.btnClose.rightTop = XY(SCREEN_WIDTH, 0);
    
    [self.labelTitle fitTitle:@"Política de privacidad " variable:0];
    self.labelTitle.centerXTop = XY(SCREEN_WIDTH/2, 55);
    
    self.labelAgreement.leftTop = XY(30, self.labelTitle.bottom+30);
    
    self.btnConfirm.centerXTop = XY(SCREEN_WIDTH/2, self.labelAgreement.bottom+40);
    
    self.viewBack.height = self.btnConfirm.bottom+16+[GlobalMethod getBottomSafeAreaHeight];
    self.viewBack.leftBottom = XY(0, SCREEN_HEIGHT);
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.viewBack.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(16, 16)];
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.path = path.CGPath;
    self.viewBack.layer.mask = maskLayer;
}


@end
